package com.example.mannortycoon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.auth0.jwt.JWT;
import com.example.mannortycoon.entity.UserCropEntity;
import com.example.mannortycoon.service.UserCropService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.UserCropVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;

@RestController
@RequestMapping("game/userCrop")
@Api(tags="用户作物")
public class UserCropController {

    private final UserCropService userCropService;

    @Autowired
    public UserCropController(UserCropService userCropService){
        this.userCropService = userCropService;
    }

    /**
     * app用户购买作物
     */
//    @PutMapping("/buyCrop")
//    @ApiOperation("用户购买作物")
//    public R buyCrop(HttpServletRequest request,@RequestBody UserCropEntity userCropEntity){
//        Long userId = JWTUtils.getId(request.getHeader("token"));
//        return userCropService.buyCrop(userId,userCropEntity);
//    }

    /**
     * app出售作物
     */
    @PostMapping("/sellCrop/{cropId}/{number}")
    @ApiOperation("出售作物")
    public R sellCrop(HttpServletRequest request,@PathVariable("cropId") Long cropId,@PathVariable("number") Integer number){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userCropService.sellCrop(userId,cropId,number);
    }


    /**
     * app获取用户作物列表
     */
    @GetMapping("/getUserCropList")
    @ApiOperation("app获取用户作物列表")
    public R getUserCropList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<UserCropVo> userCropList = userCropService.getUserCropList(userId);
        return R.ok().put("data",userCropList);
    }

    /**
     * 后台保存用户作物列表
     */
    @PostMapping("/save/{userId}")
    @ApiIgnore
    public R save(@PathVariable("userId") Long userId,@RequestBody List<UserCropEntity> userCropList){
		userCropService.saveUserCropList(userId,userCropList);
        return R.ok();
    }


    /**
     * app赠送作物
     */
    @ApiOperation("app赠送作物")
    @PostMapping("/give/{userId}/{cropId}/{number}")
    public R give(@PathVariable("userId") Long userId2,@PathVariable("cropId") Long cropId,@PathVariable("number") Integer number,HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userCropService.give(userId,userId2,cropId,number);
    }
}
